<template>
<!-- 添加/修改广告 -->
  <div style="text-align: left">
    <el-button class="add-button" type="primary" @click="dialogFormVisible = true">添加广告</el-button>
    <el-dialog
      title="添加/修改广告"
      :visible.sync="dialogFormVisible"
      @close="clear">
      <el-form :model="form" :rules="rules" style="text-align: left" ref="dataForm">
        <el-form-item label="广告位名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off" ></el-input>
        </el-form-item>
         <el-form-item label="广告宣传语" :label-width="formLabelWidth" prop="slogan">
          <el-input v-model="form.slogan" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="位置" :label-width="formLabelWidth" prop="position">
          <el-input v-model="form.position" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权重" :label-width="formLabelWidth" prop="weight">
          <el-input type="number" v-model="form.weight" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="内部url地址" :label-width="formLabelWidth" prop="press">
          <el-input v-model="form.path" autocomplete="off"></el-input>
        </el-form-item>

         <el-form-item label="外部url地址" :label-width="formLabelWidth" prop="press">
          <el-input v-model="form.tourl" autocomplete="off"></el-input>
        </el-form-item>

        <!-- 引入上传图片组件 -->
        <el-form-item label="封面" :label-width="formLabelWidth" prop="imageUrl">
          <el-input v-model="form.imageUrl" autocomplete="off" placeholder="图片 URL"></el-input>
          <img-upload @onUpload="uploadImg" ref="imgUpload"></img-upload>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibbuttonle = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import ImgUpload from './ImgUpload'
  export default {
    name: 'BannerAddEdit',
    components: {ImgUpload},
    data () {
      return {
        dialogFormVisible: false,
        username: this.$store.state.username,
        rules: {
          name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
          slogan: [{ required: true, message: '广告宣传语不能为空', trigger: 'blur' }],
          position: [{ required: true, message: '位置不能为空', trigger: 'blur' }],
          weight: [{ required: true, message: '权重不能为空', trigger: 'blur' }],
          imageUrl: [{ required: true, message: '请上传图片', trigger: 'blur' }]
        },
        form: {
          id: '',
          name: '',
          slogan: '',
          position: '',
          weight: '',
          path: '',
          tourl: '',
          imageUrl: ''
        },
        formLabelWidth: '120px'
      }
    },
    methods: {
      clear () {
        this.$refs.imgUpload.clear()
        this.form = {
          id: '',
          name: '',
          slogan: '',
          position: '',
          weight: '',
          path: '',
          tourl: '',
          imageUrl: ''
        }
      },
      onSubmit () {
        this.$axios
          .post('/admin/content/banner', {
            id: this.form.id,
            username: this.username,
            name: this.form.name,
            slogan: this.form.slogan,
            position: this.form.position,
            weight: this.form.weight,
            path: this.form.path,
            tourl: this.form.tourl,
            imageUrl: this.form.imageUrl
          }).then(resp => {
            if (resp && resp.data.code === 200) {
              this.dialogFormVisible = false
              this.$emit('onSubmit')
              // 从后端返回信息
              this.$alert(resp.data.message, '提示', {
                type: 'success',
                dangerouslyUseHTMLString: true
              })
            } else {
              // 从后端返回错误信息
              this.$alert(resp.data.message, '错误提示', {
                type: 'error',
                dangerouslyUseHTMLString: true
              })
            }
        })
      },
      uploadImg () {
        this.form.imageUrl = this.$refs.imgUpload.url
      }
    }
  }
</script>

<style scoped>
  .add-button {
     margin: 0px 0 5px 15px;
  }
</style>
